import QtQuick
import "./MyString.js" as MyString

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")


    Component.onCompleted: {
        testChange();
        testSearch();
        testSwitch();
        testDateTime();
        testMyString();
    }
    function testChange(){
        var text = "hello world";
        var tmp;
        console.log("length:",text.length); //11


        //如果省略第二个参数，则会截取到后边所有的
        //也可以传递一个负数作为参数，负数的话将会从后边计算
        tmp = text.slice(1,3); //截取[1,3)  含位置1字符，不含3 不影响原字符串
        console.log("slice:",tmp); // el

        tmp = text.slice(1); //截取1到最后
        console.log("slice:",tmp); // ello world

        tmp = text.slice(-4,-1); //从倒数4 截取到 倒数1
        console.log("slice:",tmp); // orl


        //substring 和 slice类似，区别是不能用负值作参数，如果传递了一个负值，则默认使用0
        tmp = text.substring(1,6);
        console.log("substring:",tmp); // ello

        tmp = text.substr(2,3); //(start,len) 从start位置截取 len个字符
        console.log("substring:",tmp); // llo


        //字符串替换
        tmp = text.replace("hello","你好");
        console.log("replace:",tmp); // 你好 world
    }

    function testSearch(){
        var text = "hello world";

        console.log("indexOf:",text.indexOf("l"));      // 2 从左到右搜索
        console.log("indexOf:",text.indexOf("l",5));    // 9 指定位置往右搜索
        console.log("lastIndexOf:",text.lastIndexOf("l")); // 9  从右往前

        console.log("search:",text.search("l"));        // 2  与indexOf不同，支持更强大的正则表达式，不支持指定位置

        console.log(text.charAt(1));        //e  返回指定位置字符
        console.log(text[1]);               //e  返回指定位置字符
        console.log(text.charCodeAt(1));    //101 返回指定位置的字符编码
        console.log(text.concat(" haha"));  //hello world haha   字符串拼接


        console.log(text.startsWith("hello"));  //true 是否以指定字符串开始
        console.log(text.endsWith("world"));    //true 是否以指定字符串结尾
        console.log(text.includes("world"));    //true 是否包含指定字符串
    }

    function testSwitch(){
        var text = " Hello world ";
        console.log(text.length); //13
        console.log(text.trim());        //"Hello world"
        console.log(text.toUpperCase()); //" HELLO WORLD "
        console.log(text.toLowerCase()); //" hello world "

        var num = 10;
        console.log(num.toString());    // "10"

        console.log(String("第%1行,%2列").arg(1).arg(2)); //"第1行,2列"

        console.log(num.toString().padStart(5,'0'));    //前补位 00010

        console.log(num.toString().padEnd(5,'a'));      //后补位 10aaa
    }

    function testDateTime(){
        console.log(Qt.formatDateTime(new Date(),"yyyy-MM-dd HH:mm:ss.zzz")) //2022-07-30 14:43:31.551
    }

    function testMyString(){
        var text = "hello world";

        var tmp = "";

        if(MyString.insert(text,2,"ee") === "heeello world"){
            console.log("insert pass");
        }else{
            console.log("insert failed");
        }

        if(MyString.insert(text,0,"ee") === "eehello world"){
            console.log("insert pass");
        }else{
            console.log("insert failed");
        }

        if(MyString.insert(text,10,"ee") === "hello worleed"){
            console.log("insert pass");
        }else{
            console.log("insert failed");
        }

        if(MyString.insert(text,15,"ee") === "hello worldee"){
            console.log("insert pass");
        }else{
            console.log("insert failed");
        }

        tmp = MyString.remove(text,1,3);
        if(tmp === "ho world"){
            console.log("remove pass:",tmp);
        }else{
            console.log("remove failed:",tmp);
        }

        tmp = MyString.remove(text,15,3);
        if(tmp === "hello world"){
            console.log("remove pass:",tmp);
        }else{
            console.log("remove failed:",tmp);
        }

        tmp = MyString.remove(text,10,3);
        if(tmp === "hello worl"){
            console.log("remove pass:",tmp);
        }else{
            console.log("remove failed:",tmp);
        }

        tmp = MyString.remove(text,1,5);
        if(tmp === "hworld"){
            console.log("remove pass:",tmp);
        }else{
            console.log("remove failed:",tmp);
        }

        tmp = MyString.remove(text,5,1);
        if(tmp === "helloworld"){
            console.log("remove pass:",tmp);
        }else{
            console.log("remove failed:",tmp);
        }

    }
}
